package com.csoa.community.requests;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import com.csoa.community.bean.EmailBean;
import com.csoa.community.net.ErrorMsg;
import com.csoa.community.singletons.GlobalVars;
import com.csoa.community.utils.Json;
import com.csoa.community.utils.StringUtils;

public class EmailListRequest extends AbstractRequest {

	public List<EmailBean> emails;
	public EmailListRequest(List<BasicNameValuePair> requestValues) {
		super(EMAIL_LIST_METHOD, requestValues,GlobalVars.serviceEmailURL,GlobalVars.soapEmailAction);
	}
	
	public EmailListRequest() {
		
	}

	

	@Override
	protected void parseRemaingInfo(String json) throws ErrorMsg, JSONException {
		if(!StringUtils.isEmpty(json)) {
			String strJson = "{value:"+json +"}";
			Json jsonOffis = new Json(strJson);
			emails = new ArrayList<EmailBean>();
			
			fillOfficialDocList(jsonOffis,emails);
		}
		
	}
	
	
	
	
	public void fillOfficialDocList(Json json, List<EmailBean> officialDocs) {
		Json[] jsons = json.getJsonArray("value");
		if(jsons != null) {
			for (int i=0;i<jsons.length;i++) {
				EmailBean email = new EmailBean();
				email.MessageID = jsons[i].getString("MessageID");
				email.Sender = jsons[i].getString("Sender");
				email.SentTo = jsons[i].getString("SentTo");
				email.Subject = jsons[i].getString("Subject");
				email.Date = jsons[i].getString("Date");
				email.Size = jsons[i].getString("Size");
				email.IsNewMsg = jsons[i].getBoolean("IsNewMsg");
				email.BodyHtml = jsons[i].getString("BodyHtml");
				email.Cc = jsons[i].getString("Cc");
				Json[] jsonAttachs = jsons[i].getJsonArray("Attachments");
				officialDocs.add(email);
			}
		}
		
	}
}
